<template>
  <div id="app">
    <router-view></router-view>
     <el-dialog :title="$t('login.change_Password')" :visible.sync="dialogFormVisible">
      <el-form :model="ruleForm2" status-icon :rules="rules2" ref="ruleForm2" label-width="200px" class="demo-ruleForm">
        <el-form-item :label="$t('login.old_password')" prop="age">
          <el-input  type='password' v-model.number="ruleForm2.age"></el-input>
        </el-form-item>
        <el-form-item :label="$t('login.new_password')" prop="pass">
          <el-input type="password" v-model="ruleForm2.pass" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item :label="$t('login.confirm_password')" prop="checkPass">
          <el-input type="password" v-model="ruleForm2.checkPass" auto-complete="off"></el-input>
        </el-form-item> 
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm2')" :loading='loading'>{{$t('common.Confirm')}}</el-button>
          <el-button @click="dialogFormVisible = false">{{$t('device.Cancel')}}</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import evBus from '@/utils/bus';
import {changePwd} from '@/api/administrator';

  export default{
    name: 'App',
     data () {
    var checkAge = (rule, value, callback) => {
        if (!value) {
          return callback(new Error('请输入原密码'));
        }
        callback()
      };
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入新密码'));
        } else {
          if (this.ruleForm2.checkPass !== '') {
            this.$refs.ruleForm2.validateField('checkPass');
          }
          callback();
        }
      };
      var validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.ruleForm2.pass) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
    return {
      dialogFormVisible: false,
      ruleForm2: {
          pass: '',
          checkPass: '',
          age: ''
        },
        rules2: {
          pass: [
            { validator: validatePass, trigger: 'blur' }
          ],
          checkPass: [
            { validator: validatePass2, trigger: 'blur' }
          ],
          age: [
            { validator: checkAge, trigger: 'blur' }
          ]
      },
      loading: false
    }
  },
  mounted () {
;
    evBus.$on('changePwd', () => {
      this.dialogFormVisible = true
    })
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate( async (valid) => {
        if (valid) {
          this.loading = true
          let res
          try {
            res = await changePwd({
              oldPassWord: this.ruleForm2.age,
              passWord: this.ruleForm2.pass
            })
          } catch (error) {
            this.loading = false
          }
          this.loading = false
      
          if (res.code == 1) {
            this.$message({
              type: 'success',
              message: '修改成功，需要重新登录，2秒后跳转登录页面'
            })
            this.dialogFormVisible = false
            setTimeout(() => {
              this.$store.dispatch('FedLogOut').then(() => {
                location.reload()// In order to re-instantiate the vue-router object to avoid bugs
              })
            }, 2000);
           
          } else {
            this.$message({
              type: 'error',
              message: res.data.message
            })
          }
          console.log(res);
        } else {
          this.$message({
            type: 'warning',
            message: '请填写完整的表单'
          })
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
  }
</script>
